package br.com.jbssistemas.loterica.daos;

import br.com.jbssistemas.loterica.hibernate.HibernateUtil;
import static br.com.jbssistemas.loterica.hibernate.HibernateUtil.getSession;
import java.io.Serializable;
import java.util.List;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

/**
 *
 * @author daniel
 * @param <T>
 *
 */
public class DAOGenerico<T> {

    private Class<T> entityClass;

    public DAOGenerico(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public void save(T entity) {

        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();

        session.save(entity);

        transaction.commit();

        session.close();

    }

    public void update(T entity) {

        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();

        session.update(entity);

        transaction.commit();

        session.close();

    }

    public void saveOrUpdate(T entity) {

        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();

        session.saveOrUpdate(entity);

        transaction.commit();

        session.close();

    }

    public void delete(T entity) {

        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();

        session.delete(entity);

        transaction.commit();

        session.close();

    }

    public T getById(Serializable id) {

        Session ses = getSession();

        ses.setCacheMode(CacheMode.IGNORE);

        Serializable toReturn = (Serializable) ses.get(entityClass, id);

        ses.close();

        return (T) toReturn;
    }

    public <T extends Serializable> T findPojo(String query, Object... params) {

        Session ses = HibernateUtil.getSession();

        ses.setCacheMode(CacheMode.IGNORE);

        org.hibernate.Query qr = ses.createQuery(query);

        qr.setMaxResults(1);

        for (int i = 0; i < params.length; i++) {
            qr.setParameter(i, params[i]);
        }

        Serializable toReturn = (Serializable) qr.uniqueResult();

        ses.close();

        return (T) toReturn;
    }

    public List<T> findAll() {

        Session session = HibernateUtil.getSession();

        Criteria criteria = session.createCriteria(entityClass);

        criteria.addOrder(Order.asc("id"));

        List<T> list = criteria.list();

        session.close();

        return list;

    }

    protected <T extends Serializable> List<T> find(String query, Object... params) {

        Session session = HibernateUtil.getSession();

        session.setCacheMode(CacheMode.IGNORE);

        org.hibernate.Query qr = session.createQuery(query);

        for (int i = 0; i < params.length; i++) {
            qr.setParameter(i, params[i]);
        }

        List<T> list = qr.list();

        session.close();

        return list;

    }

}
